package com.lizk.study.netty.day009;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketOption;
import java.net.SocketOptions;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

/**
 * ServerSocket服务端
 * @author lizhikui
 * @date 2019/6/30 11:29
 */
public class NioTest12 {
    public static void main(String[] args) throws IOException {
        Server server = new Server();
        server.startServer();

    }

    static class Server {
        public void startServer() throws IOException {
            ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
            serverSocketChannel.configureBlocking(false); //默认值为ture，这里必须设置false才是使用非阻塞模式
            serverSocketChannel.setOption(StandardSocketOptions.SO_REUSEADDR,true);
            serverSocketChannel.bind(new InetSocketAddress(9999));
            System.out.println("--------------------");
            Selector selector = Selector.open();
            System.out.println("--------------------");
            serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
            while (true) {
                System.out.println("执行select方法");
                int count = selector.select();
                if (count <= 0) {
                    continue;
                }
                Set<SelectionKey> selectionKeys = selector.selectedKeys();
                Iterator<SelectionKey> iterator = selectionKeys.iterator();
                while (iterator.hasNext()) {
                    SelectionKey selectionKey = iterator.next();
                    if (selectionKey.isAcceptable()) {
                        try {
                            ServerSocketChannel tmp = (ServerSocketChannel) selectionKey.channel();
                            SocketChannel socketChannel = tmp.accept();
                            if (socketChannel == null) {
                                System.out.println("断开连接");
                            } else {
                                socketChannel.configureBlocking(false);
                                System.out.println("--------------------");
                                socketChannel.register(selector, SelectionKey.OP_READ);
                                iterator.remove();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }else if(selectionKey.isReadable()){
                        try {
                            ByteBuffer b = ByteBuffer.allocate(1024);
                            SocketChannel tmp = (SocketChannel) selectionKey.channel();
                            System.out.println("---------"+tmp.isConnectionPending());
                            tmp.read(b);
                            b.flip();
                            byte[] bytes = new byte[1024];
                            b.get(bytes,0,b.remaining());
                            b.flip();
                            tmp.write(b);
                            iterator.remove();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                selectionKeys.clear();
            }
        }
    }

}
